package www.nomina.controller.bean.sat;

import java.io.File;
import java.io.FileInputStream;
import java.net.InetAddress;
import java.net.URL;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.HashSet;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;

import www.nomina.controller.beans.empleadoBean;
import www.nomina.controller.beans.empresaBean;
import mx.gob.sat.timbrefiscaldigital.TimbreFiscalDigital;

import com.buzonfiscal.ns.xsd.bf.requesttimbracfdi.Documento;
import com.buzonfiscal.ns.xsd.bf.requesttimbracfdi.InfoBasica;
import com.buzonfiscal.ns.xsd.bf.timbradocfd.RequestTimbradoCFD;
import com.buzonfiscal.timbradocfdi.TimbradoCFDI;
import com.buzonfiscal.timbradocfdi.TimbradoCFDI_Service;



public class TimbreFromRequestObject implements HostnameVerifier{	

	empresaBean empresa = new empresaBean();
	empleadoBean empleado = new empleadoBean();
	
	public static String base64 = "PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4NCg0KPGNmZGk6Q29tcHJvYmFudGUgTnVtQ3RhUGFnbz0iMjMyMiIgTHVnYXJFeHBlZGljaW9uPSJNT05URVJSRVksIE4uTC4iIG1ldG9kb0RlUGFnbz0iVEFSSkVUQSINCiAgICB0aXBvRGVDb21wcm9iYW50ZT0iaW5ncmVzbyIgdG90YWw9IjAuMDIxNiIgc3ViVG90YWw9IjAuMDEiIGNvbmRpY2lvbmVzRGVQYWdvPSJDT05UQURPIg0KICAgIGNlcnRpZmljYWRvPSJNSUlFSVRDQ0F3bWdBd0lCQWdJVU1EQXdNREV3TURBd01EQXhNRFF3TnpVd016Y3dEUVlKS29aSWh2Y05BUUVGQlFBd2dnRTJNVGd3TmdZRFZRUUREQzlCTGtNdUlHUmxiQ0JUWlhKMmFXTnBieUJrWlNCQlpHMXBibWx6ZEhKaFkybkRzMjRnVkhKcF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dkgyZ2xUN0lCVXVkcTFmZ005WUUrMWdIUEJvcmprK0FBakV3SEpEUHd6WG0vL2ZqUUxLVUM3SzBGNUd1VTA2R3BoSHdOUytRNVFFbWZkVzVtTXQwY2t5UEFNNG9FOC9Wb1l0b2c4PSINCiAgICAgICAgICAgIG5vQ2VydGlmaWNhZG9TQVQ9IjAwMDAxMDAwMDAwMTAyNTUzMzY4Ig0KICAgICAgICAgICAgc2VsbG9DRkQ9ImRvNmFTd0xCWmh3TVFuYlpBMUFpS05OQnBtRTlFU3RoNFFtY0lhUlJsZERVNEYyeGgzUUtSTC9oVjcxYXpMRnRBa2NTblJ6Z3dxUXBZRWdBSDg4MmwvOTdabEdyTUdSNnpQWXlaT0lnU0VOTFhsNFF6UlJZMUVwYzVPa1cyWkdCUXVFYzJTcmZPOFExVWpSOERJNnN4ajRKUHBZbDRmQUZMenhVbmlTSTcwMD0iDQogICAgICAgICAgICBGZWNoYVRpbWJyYWRvPSIyMDEyLTExLTI3VDEyOjE3OjI1IiBVVUlEPSI4NTlkNjZlZC1kYzNkLTRmN2EtOWQyZC1hZWMwYmQ4NzVmNmYiDQogICAgICAgICAgICB2ZXJzaW9uPSIxLjAiLz4NCiAgICA8L2NmZGk6Q29tcGxlbWVudG8+DQogICAgPGNmZGk6QWRkZW5kYT4NCiAgICAgICAgPGJmYTI6QWRkZW5kYUJ1em9uRmlzY2FsIHZlcnNpb249IjIuMCI+DQogICAgICAgICAgICA8YmZhMjpUaXBvRG9jdW1lbnRvIGRlc2NyaXBjaW9uPSJGYWN0dXJhIiBub21icmVDb3J0bz0iRkFDIi8+DQogICAgICAgIDwvYmZhMjpBZGRlbmRhQnV6b25GaXNjYWw+DQogICAgPC9jZmRpOkFkZGVuZGE+DQo8L2NmZGk6Q29tcHJvYmFudGU+";
	//public String rfcReceptor=empleado.getRfc();
	public static String rfcEmisor = "BES051214PB6";
	public static String rfcReceptor = "";
	public static String url = "https://testperf.diverza.com/timbrador/timbrado";
	public static String password = "BES051214PB6";
	//Datos de la empresa que se registro
	//public String rfcEmisor=empresa.getRfc();
	//public String pathCert=empresa.getPfx();
	//public String password=empresa.getPassword();
	public static String pathCert = "/home/nastick/Escritorio/BES051214PB6/BES051214PB6.pfx";

	public static void main(String args[]) {
        try {
        	
        	
        	//System.setProperty("javax.net.ssl.trustStore","/home/nastick/Escritorio/BES051214PB6/BES051214PB6.pfx");
        	System.setProperty("Dsun.security.ssl.allowUnsafeRenegotiation",base64 );
        	//conexion SSL        	
        	KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509"); 
			//para certificados pfx utlizar instancia PKCS12, para certificados JKS utilizar instancia JKS
        	
        	KeyStore keyStore = KeyStore.getInstance("PKCS12"); 
        	keyStore.load(new FileInputStream(new File(pathCert)), password.toCharArray()); 
			
        	//carga el certificado y password
			keyManagerFactory.init(keyStore, password.toCharArray()); 
					
			//se realiza instancia de contexto SSL
			SSLContext sslContext = SSLContext.getInstance("SSL"); 
			sslContext.init(keyManagerFactory.getKeyManagers(), null, new SecureRandom()); 
			
			//se agrega a la peticion la factoria del socket en contexto SSL
			HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
			HttpsURLConnection.setDefaultHostnameVerifier(new TimbreFromRequestObject());
			
			//crea objeto request
			RequestTimbradoCFD requestTimbradoCFD = new RequestTimbradoCFD();
			
			//@infoBasica
			requestTimbradoCFD.setInfoBasica(new InfoBasica());
				//@rfcEmisor
				requestTimbradoCFD.getInfoBasica().setRfcEmisor(rfcEmisor);
				//@rfcReceptor
				requestTimbradoCFD.getInfoBasica().setRfcReceptor(rfcReceptor);
			//@documento
			requestTimbradoCFD.setDocumento(new Documento());
				//@archivo
				requestTimbradoCFD.getDocumento().setArchivo(base64.getBytes());
			
			//url servicio
			URL urlWSDL = new URL(url);
			
			//instancia de objetos de ws
			TimbradoCFDI_Service service = new TimbradoCFDI_Service(urlWSDL);
			TimbradoCFDI port = service.getTimbradoCFDISOAP();
			
			//realiza peticion al timbre
			TimbreFiscalDigital timbreFiscalDigital = port.timbradoCFD(requestTimbradoCFD);
			
			//verifica resultados
    		System.out.println(".:: getNoCertificadoSAT " + timbreFiscalDigital.getNoCertificadoSAT());
			System.out.println(".:: getSelloCFD " + timbreFiscalDigital.getSelloCFD());
			System.out.println(".:: getSelloSAT " + timbreFiscalDigital.getSelloSAT());
			System.out.println(".:: getUUID " + timbreFiscalDigital.getUUID());
			System.out.println(".:: getFechaTimbrado " + timbreFiscalDigital.getFechaTimbrado());
        }catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	public boolean verify(String hostname, SSLSession session) {
		try {
			if ((new HashSet<InetAddress>(Arrays.asList(
					InetAddress.getAllByName(hostname))).contains(
							InetAddress.getByName(session.getPeerHost()))))
				return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
}
